package com.example.yls.demoa;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private TextView tvResult;
    RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        requestQueue = Volley.newRequestQueue(MainActivity.this);

        tvResult = findViewById(R.id.tv_result);
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                useVolleyStringRequest("13229518262");
            }
        });
        findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                useVolleyStringRequestPost("15642419274");
            }
        });

        findViewById(R.id.btn3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                useVolleyJsonRequest();

            }
        });
    }

    private void useVolleyJsonRequest() {
        String url = "http://t.weather.sojson.com/api/weather/city/101280101";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null,
                new Response.Listener<JSONObject>() {


                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.e(TAG, jsonObject.toString());
                        try {
//                            JSONArray jsonArray = new JSONArray(jsonObject.toString());

                            JSONArray forecast = jsonObject.getJSONObject("data")
                                    .getJSONArray("forecast");

                            StringBuffer stringBuffer = new StringBuffer();

                            for (int i = 0; i < forecast.length(); i++) {
                                JSONObject data = forecast.getJSONObject(i);

                                Log.e(TAG, "data = " + data.getString("ymd"));
                                stringBuffer.append(data.getString("ymd")).append("\n");
                                stringBuffer.append(data.getString("fl")).append("\n");
                                stringBuffer.append(data.getString("fx")).append("\n");
                                stringBuffer.append(data.getString("sunset")).append("\n");
                                stringBuffer.append(data.getString("high")).append("\n");
                                stringBuffer.append(data.getString("sunrise")).append("\n");
                                stringBuffer.append(data.getString("notice")).append("\n");
                                stringBuffer.append(data.getString("type")).append("\n");
                                stringBuffer.append(data.getString("low")).append("\n");
                                stringBuffer.append(data.getString("date")).append("\n");
                                stringBuffer.append(data.getString("week")).append("\n");
                                stringBuffer.append(" ").append("\n");

                                tvResult.setText(stringBuffer.toString());
                            }


//
//                           String wendu = jsonObject.getJSONObject("data").getString("wendu");
//                            String quality = jsonObject.getJSONObject("data").getString("quality");
//                            String ganmao = jsonObject.getJSONObject("data").getString("ganmao");
//                            int pm25 = jsonObject.getJSONObject("data").getInt("pm25");
////                            StringBuffer stringBuffer = new StringBuffer();
//                           stringBuffer.append("温度:").append(wendu).append("℃").append("\n");
//                           stringBuffer.append("空气质量:").append(quality).append("\n");
//                           stringBuffer.append("感冒:").append(ganmao).append("\n");
//                           stringBuffer.append("PM2.5:").append(pm25).append("\n");


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        });

        requestQueue.add(jsonObjectRequest);
    }

    private void useVolleyStringRequestPost(final String phone) {
        String phoneBaseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";

        StringRequest stringRequest = new StringRequest(Request.Method.POST, phoneBaseUrl,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        Log.e(TAG, s);
//                主线程
                        tvResult.setText(s);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("mobileCode", phone);
                map.put("userID", "");

                return map;
            }
        };
        requestQueue.add(stringRequest);

    }


    private void useVolleyStringRequest(String phone) {
        String url = "http://i.mooc.chaoxing.com/space/index?t=1552608509697";
        String phoneBaseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String phoneUrl = phoneBaseUrl.concat("?mobileCode=").concat(phone).concat("&userID= ");
        StringRequest stringRequest = new StringRequest(phoneUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.e(TAG, s);
//                主线程
                tvResult.setText(s);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        });
        requestQueue.add(stringRequest);

    }
}
